<?php

 //***************************
    //  授权登录接口
    //***************************
    public function authlogin() {
        $openid = $_POST['openid'];
        if (!$openid) {
            echo json_encode(array('status' => 0, 'err' => '授权失败！' . __LINE__));
            exit();
        }
        $con = array();
        $con['openid'] = trim($openid);
        $uid = M('user')->where($con)->getField('id');
        if ($uid) {
            $userinfo = M('user')->where('id=' . intval($uid))->find();
            if (intval($userinfo['del']) == 1) {
                echo json_encode(array('status' => 0, 'err' => '账号状态异常！'));
                exit();
            }
            $err = array();
            $err['ID'] = intval($uid);
            $err['NickName'] = $_POST['NickName'];
            $err['HeadUrl'] = $_POST['HeadUrl'];
            echo json_encode(array('status' => 1, 'arr' => $err));
            exit();
        } else {
            $is_fx = M('user')->where('id',$_POST['share_userId'])->getField('is_fx');
            if($is_fx == 2){//判断分享者是否是注册成为分享
                $data['pid'] = $_POST['share_userId'];
            }
            $data = array();
            $data['name'] = $_POST['NickName'];
            $data['uname'] = $_POST['NickName'];
            $data['photo'] = $_POST['HeadUrl'];
            $data['sex'] = $_POST['gender'];
            $data['openid'] = $openid;
            $data['source'] = 'wx';
            $data['addtime'] = time();
            if (!$data['openid']) {
                echo json_encode(array('status' => 0, 'err' => '授权失败！' . __LINE__));
                exit();
            }
            $res = M('user')->add($data);
            if ($res) {
                $err = array();
                $err['ID'] = intval($res);
                $err['NickName'] = $data['name'];
                $err['HeadUrl'] = $data['photo'];
                echo json_encode(array('status' => 1, 'arr' => $err));
                exit();
            } else {
                echo json_encode(array('status' => 0, 'err' => '授权失败！' . __LINE__));
                exit();
            }
        }
    }
	
	
	
	
	
	//***************************
    //  获取sessionkey 接口
    //***************************
    public function getsessionkey() {
        $wx_config = C('weixin');
        $appid = $wx_config['appid'];
        $secret = $wx_config['secret'];

        $code = trim($_POST['code']);
        if (!$code) {
            echo json_encode(array('status' => 0, 'err' => '非法操作！'));
            exit();
        }

        if (!$appid || !$secret) {
            echo json_encode(array('status' => 0, 'err' => '非法操作！' . __LINE__));
            exit();
        }

        $get_token_url = 'https://api.weixin.qq.com/sns/jscode2session?appid=' . $appid . '&secret=' . $secret . '&js_code=' . $code . '&grant_type=authorization_code';
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $get_token_url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
        $res = curl_exec($ch);
        curl_close($ch);
        echo $res;
        exit();
    }

    //***************************
    //  获取sessionkey 接口
    //***************************
    public function getsessionkeys() {
        $wx_config = C('weixin');
        $appid = 'wx5e3dd81af8bf352a';
        $secret = '30421d00aaee201bc0400994defbeed7';

        $code = trim($_POST['code']);
        if (!$code) {
            echo json_encode(array('status' => 0, 'err' => '非法操作！'));
            exit();
        }
        $get_token_url = 'https://api.weixin.qq.com/sns/jscode2session?appid=' . $appid . '&secret=' . $secret . '&js_code=' . $code . '&grant_type=authorization_code';
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $get_token_url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
        $res = curl_exec($ch);
        curl_close($ch);
        echo $res;
        exit();
    }